package com.qf.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 根据当前类，设置授权规则的方式，根据ip地址，做黑白名单选项
 * @author zhaojian
 */

@Configuration
public class BlackAndWhiteNamesConfig implements RequestOriginParser {

    public static final String LOGIN_TOKEN_KEY = "X-Dts-Admin-Token";//令牌：用户名
    /**
     * 用户名
     * @param httpServletRequest
     * @return
     */
/*    @Override
    public String parseOrigin(HttpServletRequest httpServletRequest) {

        Cookie[] cookies = httpServletRequest.getCookies();
        for (Cookie cookie : cookies) {
            if("jessionid".equals(cookie.getName())){
                return cookie.getValue();// 用户名  令牌  信物
            }
        }
        String username = httpServletRequest.getHeader(LOGIN_TOKEN_KEY);
        return username;
    }*/

    /**
     * IP 授权校验  黑白名单
     * @param request
     * @return
     */
    @Override
    public String parseOrigin(HttpServletRequest request) {
        // 根据ip地址,做黑白名单
        String ip = request.getRemoteAddr();//有代理？
        System.out.println("ip:" + ip);
        return ip;
    }
}